/**
 * Creat For 11643
 * CreatTime 2022/5/20
 * Description:设置服务器ip地址
 */
import 'dart:convert';
import 'dart:io';

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:schoolassets/Tools%E5%B7%A5%E5%85%B7/requestUrl.dart';
import 'package:shared_preferences/shared_preferences.dart';


import 'LoadToast.dart';
import 'ShowToast.dart';

class Setting extends StatefulWidget {
  const Setting({Key? key}) : super(key: key);

  @override
  State<Setting> createState() => _SettingState();
}

class _SettingState extends State<Setting> {
  // 服务器ip
  var IP;
  TextEditingController IPController = new TextEditingController();
  @override
  void initState() {
    super.initState();
    Future.delayed(Duration.zero, () async {
      // 获取本地设置的ip
      SharedPreferences sharedPreferences =
      await SharedPreferences.getInstance();
      // 获取本地ip地址
      var Url = sharedPreferences.getString('ApiUrl');
      if (sharedPreferences.getString('ApiUrl') == null) {
        // 如果没有设置ip
        IP = "http://192.168.43.147:1090";
      } else {
        IP = Url;
      }
      IPController = TextEditingController(text: IP);
      setState(() {});
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("设置服务器地址"),
        centerTitle: true,
      ),
      body: Container(
        padding: EdgeInsets.all(15),
        margin: EdgeInsets.only(top: 30),
        child: Column(
          children: [
            Text(
              "请按照示例设置服务器地址!",
              style: TextStyle(fontSize: 25),
            ),
            Container(
              height: 15,
            ),
            Container(
              child: Row(
                children: [
                  Expanded(
                      child: TextField(
                        controller: IPController,
                        onChanged: (value) {
                          IP = value;
                        },
                        decoration: InputDecoration(
                            hintText: "示例:http或https://PC端IP地址:端口号",
                            hintStyle:
                            TextStyle(fontSize: 13, color: Colors.black26)),
                      )),
                  Container(
                    width: 5,
                  ),
                  MaterialButton(
                    onPressed: () {
                      // 调用测试接口进行测试通信
                      Test();
                    },
                    color: Colors.blue,
                    child: Text(
                      "确定",
                      style: TextStyle(color: Colors.white),
                    ),
                  )
                ],
              ),
            )
          ],
        ),
      ),
    );
  }

  // 调用测试接口测试通信
  Future<void> Test() async {
    var ResponseData;
    try {
      // 加载框
      LoadToast().LoadingToast("设置中...", context);
      BaseOptions options = new BaseOptions();
      // options.connectTimeout = 5000;//超时时间
      // dio.options = options;
      print("接口地址:$IP/api/Public/TestNet");
      ResponseData = await dio.get(IP+'/api/Public/TestNet',queryParameters: {});
      print("接口数据:$ResponseData");
      if (ResponseData.statusCode == 200) {
        print("接口数据:$ResponseData");
        ResponseData = json.decode(ResponseData.toString());
        // 请求成功
        if (ResponseData['code'] == 0) {
          //  通信成功
          SharedPreferences sharedPreferences =
          await SharedPreferences.getInstance();
          sharedPreferences.setString("ApiUrl", IPController.text);
          LoadToast().CloseLoading();
          sleep(Duration(milliseconds: 80));
          ShowToast().MyToast("设置成功!");
          Navigator.pop(context);
        } else {
          LoadToast().CloseLoading();
          ShowToast().MyToast("服务器IP地址不正确!");
        }
      } else {
        LoadToast().CloseLoading();
        // 请求失败
        ShowToast().MyToast("服务器IP地址不正确!");
      }
    } catch (e) {
      LoadToast().CloseLoading();
      print("异常信息:$e");
      ShowToast().MyToast("服务器IP地址设置失败!");
    }
  }
}

